package com.zyy;

import javax.xml.crypto.Data;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Date;

public class demoData {
//    一共7种方法
    public static void main(String[] args) {
//        当前时间截
        Date date = new Date();
        System.out.println(date.getTime());
        System.out.println(System.currentTimeMillis());
//
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateStr = df.format(date);
        System.out.println(dateStr);
        System.out.println("jdk1.8以后");
//        jdk1.8以后
        LocalDate now = LocalDate.now();
        System.out.println(now);
        System.out.println(now.getYear());
        System.out.println(now.getDayOfYear());//
        System.out.println(now.getDayOfMonth());
        LocalTime now1 = LocalTime.now();
        System.out.println(now1);

        LocalDateTime nowtime = LocalDateTime.now();
        LocalDateTime dateTime = LocalDateTime.of(2023, 10, 1, 12, 0);
        System.out.println("nowtime"+nowtime);
        System.out.println(dateTime);
        System.out.println(nowtime.getYear());

//
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String formatted = nowtime.format(formatter);
        System.out.println(formatted);
        System.out.println(nowtime.plusDays(1));
        System.out.println(nowtime.minusDays(1));

        LocalDateTime newDateTime = nowtime.withYear(2024);
        System.out.println(newDateTime);
//
        LocalDate start = LocalDate.of(2023, 1, 1);
        LocalDate end = LocalDate.of(2023, 10, 1);
        Period period = Period.between(start, end);
        System.out.println(period.getMonths()); // 9

        LocalTime start2 = LocalTime.of(10, 0);
        LocalTime end2 = LocalTime.of(12, 0);
        Duration duration = Duration.between(start2, end2);
        System.out.println(duration.toHours()); // 2

    }
}
